package com.yaolaizaixian.merchant.base;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;

import com.umeng.analytics.MobclickAgent;
import com.yaolaizaixian.merchant.LoginActivity;
import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.utils.SharedPreferencesUtil;
import com.yaolaizaixian.merchant.utils.UIUtils;
import com.yaolaizaixian.merchant.view.StatusBarCompat;

import butterknife.ButterKnife;
import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;


/**
 * Created by xinghongfei on 16/8/12.
 */
public abstract class BaseActivity extends SwipeBackActivity {

    protected final String TAG = getClass().getSimpleName();
    public static BaseActivity activity;
    private SwipeBackLayout mSwipeBackLayout;
    private android.app.AlertDialog.Builder accountRemovedBuilder;
    private android.app.AlertDialog.Builder conflictBuilder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*布局初始化*/
        initView();
        ButterKnife.bind(this);
        mSwipeBackLayout = getSwipeBackLayout();
        // 设置滑动方向，可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL,EDGE_BOTTOM
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        //方便退出登录
        activity = this;
        ((MyApplication) UIUtils.getContext()).addActivity(this);
        //统一  状态栏  和  项目的颜色
        if (Build.VERSION.SDK_INT >= 21) {

            StatusBarCompat.compat(this);
            StatusBarCompat.compat(this, getResources().getColor(R.color.backtwo));
        }

        /*数据初始化*/
        initData();
    }

    public abstract void initView();

    public abstract void initData();

    @Override
    protected void onResume() {
        super.onResume();

        activity = this;
        //友盟统计
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();

        activity = null;
        //友盟统计
        MobclickAgent.onPause(this);
    }

    /**
     * activity退出时将activity移出栈
     */
    @Override
    protected void onDestroy() {

        super.onDestroy();

        ((MyApplication) UIUtils.getContext()).removeActivity(this);

    }

    /**
     * show the dialog when user logged into another device
     */
    protected void showConflictDialog(final Activity mcontext,String tichu) {

        if (!mcontext.isFinishing()) {
            // clear up global variables
            try {
                if (conflictBuilder == null)
                conflictBuilder = new android.app.AlertDialog.Builder(mcontext);
//              conflictBuilder.setTitle(st);
                conflictBuilder.setMessage(tichu);
                conflictBuilder.setPositiveButton("确认", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    /*  SharedPreferencesUtil.saveString(BaseActivity.this, "usersysno", "");
                        dialog.dismiss();
                        conflictBuilder = null;
                        finish();
                        Intent intent = new Intent(mcontext, LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        //intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);*/
                        SharedPreferencesUtil.saveString(BaseActivity.this, "usersysno", "");
                        dialog.dismiss();
                        conflictBuilder = null;
                        MyApplication.clearActivities();
                        //finish();
                        Intent intent = new Intent(mcontext, LoginActivity.class);
                        startActivity(intent);

                    }
                });

                conflictBuilder.setCancelable(false);
                conflictBuilder.create().show();

            }
            catch (Exception e) {
                Log.e(TAG, "---------color conflictBuilder error" + e.getMessage());
            }

        }

    }

}
